package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.bean.Category;
import com.itheima.bean.Setmeal;
import com.itheima.common.R;
import com.itheima.dto.SetmealDto;
import com.itheima.mapper.CategoryMapper;
import com.itheima.service.SetmealService;
import com.itheima.mapper.SetmealMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author 海关监管和对方
* @description 针对表【setmeal(濂楅?)】的数据库操作Service实现
* @createDate 2024-05-06 10:13:07
*/
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal>
    implements SetmealService{

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public R<Page<SetmealDto>> pageR(Page<Setmeal> setmealPage, LambdaQueryWrapper<Setmeal> wrapper) {
        setmealMapper.selectPage(setmealPage,wrapper);



        List<SetmealDto> setmealDtos = new ArrayList<>();
        List<Setmeal> setmeals = setmealPage.getRecords();
        for (Setmeal setmeal :setmeals) {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal,setmealDto);
            setmealDto.setCategoryName(categoryMapper.selectOne(new LambdaQueryWrapper<Category>()
                    .eq(Category::getId,setmeal.getCategoryId())).getName());
            setmealDtos.add(setmealDto);
        }

        Page<SetmealDto> setmealDtoPage = new Page<SetmealDto>();
        BeanUtils.copyProperties(setmealPage,setmealDtoPage);
        setmealDtoPage.setRecords(setmealDtos);

        return R.success(setmealDtoPage);
    }
}




